import pandas as pd
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
from sklearn.impute import SimpleImputer

# 加载数据
data = pd.read_csv('/Users/linshangjin/25CCM/NKU-C/t2/code_2/all_vars_yoy.csv',parse_dates=['date'])

# 选择数值型列进行标准化（排除date列）
numeric_cols = data.columns.drop('date')
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data[numeric_cols])

# 将标准化后的数据转换为DataFrame
data_scaled_df = pd.DataFrame(data_scaled, columns=numeric_cols, index=data.index)

# 对缺失值做均值填充
imputer = SimpleImputer(strategy='mean')
data_imputed = pd.DataFrame(
    imputer.fit_transform(data_scaled_df),
    columns=numeric_cols,
    index=data_scaled_df.index
)


# 初始化PCA，假设我们想要将变量减少到2个主成分
pca = PCA(n_components=3)

# 拟合PCA模型
# principal_components = pca.fit_transform(data_scaled_df)
principal_components = pca.fit_transform(data_imputed)

# 将结果转换为DataFrame
principal_df = pd.DataFrame(data=principal_components, 
                            columns=['Principal Component 1', 'Principal Component 2', 'Principal Component 3'], 
                            index=data.index)

# 查看主成分的解释方差比例
print('Explained variance ratio:', pca.explained_variance_ratio_)

# 合并原始数据和主成分数据（可选）
final_df = pd.concat([data[['date']], principal_df], axis=1)

final_df.to_csv('/Users/linshangjin/25CCM/NKU-C/t3/PCA_result.csv', index=False, float_format='%.6f')

# 显示最终DataFrame
print(final_df.head())

# 绘制主成分散点图（可选）
plt.figure(figsize=(8, 6))
plt.scatter(principal_df['Principal Component 1'], 
            principal_df['Principal Component 2'], 
            alpha=0.5)
plt.title('PCA of Dataset')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.grid(True)
plt.savefig('/Users/linshangjin/25CCM/NKU-C/t3/PCA_result.png', dpi=300)
plt.show()